package edu.jvn.ict.locationsharing;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LocationSharingRun extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	Button offlineButton;
	Button onlineButton;
	EditText displayBox;

	Timer timer = new Timer();

	Handler myHandler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

			MemberServer.activity = this;
			MemberServer.androidActivityClass = LocationSharingRun.class;

			switch (LocationSharing.option) {
			case LocationSharing.JOIN:
				MemberServer.joinGroupOf(LocationSharing.phoneNumber, LocationSharing.port);
				break;

			case LocationSharing.START:
				MemberServer.startAGroup(LocationSharing.phoneNumber, LocationSharing.port);
//				Utility.createSampleSMS(getApplicationContext());
				break;

			default:
				break;

		}
		this.setContentView(R.layout.online);
		Button leaveButton = (Button) findViewById(R.id.leaveButton);
		leaveButton.setOnClickListener(this);

		displayBox = (EditText) findViewById(R.id.displayBox);

		offlineButton = (Button) findViewById(R.id.offlineButton);
		offlineButton.setOnClickListener(this);

		onlineButton = (Button) findViewById(R.id.onlineButton);
		onlineButton.setClickable(false);
		onlineButton.setOnClickListener(this);

		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				updateDisplayBox();
			}
		}, MemberServer.intervalUpdate, MemberServer.intervalUpdate);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.onlineButton:
			MemberServer.goOnline(LocationSharing.port);
			onlineButton.setClickable(false);
			break;
		case R.id.offlineButton:
			MemberServer.goOffline();
			offlineButton.setClickable(false);
			break;
		case R.id.leaveButton:
			MemberServer.leavingGroup();
			goLoginPage();
			break;
		}
	}

	public void goOnline() {
		this.setContentView(R.layout.online);
		Button leaveButton = (Button) findViewById(R.id.leaveButton);
		leaveButton.setOnClickListener(this);

		Button offlineButton = (Button) findViewById(R.id.offlineButton);
		offlineButton.setOnClickListener(this);

		Button onlineButton = (Button) findViewById(R.id.onlineButton);
		onlineButton.setOnClickListener(this);
	}

	public void goLoginPage() {
		this.finish();
		startActivity(new Intent(this, LocationSharing.class));
	}

	public void updateDisplayBox() {
		myHandler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				displayBox.setText(generateDisplay());
			}

			public String generateDisplay() {
				String result = "";
				if (MemberServer.dictionary != null) {
					for (MemberInfo memberInfo : MemberServer.dictionary) {
						result += memberInfo.toString() + "\n";
					}
				} else {
					result = "Not yet receive Information";
				}
				return result;
			}
		});

	}

}